package com.twp.auth.rest.api.manage;

import com.twp.auth.constants.CacheConstants;
import com.twp.auth.domain.authority.RequestUrl;
import com.twp.auth.service.RequestUrlService;
import com.twp.auth.tuple.RequestUrlActionBody;
import com.twp.auth.utils.Cache;
import com.twp.common.component.process.exception.combo.IComboExecptionForTFunc;
import com.twp.common.tuple.ResultDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping(value = "/api/oauth/requestUrl",produces = "application/json; charset=utf-8")
public class RequestUrlController {

    @Autowired
    RequestUrlService requestUrlService;

    @Autowired
    IComboExecptionForTFunc iComboExecptionForTFunc;

    @Autowired
    Cache cache;

    @GetMapping("")
    ResultDTO find(@RequestParam(defaultValue = "select") String flag, String menuCode, RequestUrl requestUrl, @PageableDefault Pageable pageable){
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().message("查询requestMappingPath数据成功").build(),
                it->it.setData(requestUrlService.find(flag,menuCode,requestUrl,pageable)),
                (e,it)->{
                    it.setMessage("查询requestMappingPath数据失败");
                    it.setIsSuccess(false);
                }
        );
    }

    @GetMapping("effect")
    ResultDTO effect(){
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().message("权限缓存生效成功").build(),
                it-> {
                    cache.clearByHazelcast(CacheConstants.CACHE_URL_ROLE_KEY);
                    requestUrlService.selectAllRequestUrlForRole();
                },
                (e,it)->{
                    it.setMessage("权限缓存生效异常");
                    it.setIsSuccess(false);
                }
        );
    }
    /*
    取消requestMapping绑定
    增加requestMapping绑定
     */
    @PutMapping("action")
    ResultDTO action(@RequestBody RequestUrlActionBody actionBody){
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().message("操作requestMapping绑定成功").build(),
                it-> requestUrlService.action(actionBody),
                (e,it)->{
                    it.setMessage("操作requestMapping绑定成功异常");
                    it.setIsSuccess(false);
                }
        );
    }
}
